<template>
    <div class="login-box">
        
        <el-form ref="loginFormRef" :model="loginForm" status-icon :rules="rules" label-width="70px" class="loginForm">
            <h3>后台管理系统</h3>
            <el-form-item label="账号" prop="userName">
                <el-input v-model="loginForm.userName" autocomplete="off" />
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input v-model="loginForm.password" type="password" autocomplete="off" />
            </el-form-item>

            <el-form-item>
                <el-button class="sub-btn" type="primary" @click="submitForm(loginFormRef)">登录</el-button>

            </el-form-item>
        </el-form>
    </div>
</template>

<script lang="ts">
import { defineComponent, reactive, toRefs } from 'vue';
import {InitData} from '../types/login';
import type { FormInstance } from "element-plus";
import {login} from '../http/api';
import { useRouter } from "vue-router";

export default defineComponent({
    setup() {
        const data = reactive(new InitData);
        const router = useRouter()
        const rules= {
                userName: [
                    { required: true, message: '请输入账号', trigger: 'blur' },
                    { min: 3, max: 12, message: '账号长度需要在6到12', trigger: 'blur' },
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 3, max: 12, message: '密码长度需要在6到12', trigger: 'blur' },
                ],
            }
        const submitForm = (loginFormRef:FormInstance)=> {
            loginFormRef.validate((valid:boolean)=>{
                if(valid){
                    // alert('验证通过,发送请求与')
                    login(data.loginForm).then(res=>{
                        // console.log(res);
                        localStorage.setItem("token",res.data.token)
                        router.push('/')
                    })
                }
            })

        }
        return {
            ...toRefs(data),
            rules,
            submitForm
        }
    }
})
</script>

<style lang='scss' scoped>
.login-box {
    width: 100%;
    height: 100vh;
    background: url("../assets/bj.jpg") center center no-repeat;
    background-size: 100% 100%;
    // position:fixed;//绝对定位
    box-sizing: border-box;
    padding-top: 200px;

    .loginForm {
        width: 400px;
        padding: 20px;
        background: #fff;
        margin: 0 auto;
        border-radius: 10px;
        .sub-btn{
            width: 100%;
        }
        .h3{
            text-align: center;
            margin: 20px 0px;
        }
    }
}
</style>